<template>
  <div id="userLogin">
    <h2>登录 OJ 在线判题系统</h2>
    <a-form
      :model="form"
      auto-label-width
      label-align="left"
      style="max-width: 480px; margin: 0 auto"
      @submit-success="handleSubmit"
    >
      <a-form-item
        :rules="[{ required: true, message: '账号不能为空' }]"
        field="userAccount"
        label="账号"
      >
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item
        :rules="[
          { required: true, message: '密码不能为空' },
          { minLength: 8, message: '密码不能少于 8 位' },
        ]"
        field="userPassword"
        label="密码"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
          autocomplete="off"
        />
      </a-form-item>
      <a-form-item
        :rules="[{ required: true, message: '验证码不能为空' }]"
        field="checkCode"
        label="验证码"
      >
        <a-input
          v-model="form.checkCode"
          placeholder="请输入验证码"
          autocomplete="off"
        />
        <a-image
          class="checkCode"
          :src="checkCodeImage"
          :preview="false"
          @click="toggleCheckCode"
          width="100"
          height="30"
        />
      </a-form-item>
      <a-form-item>
        <a-space align="end" style="flex: 1; justify-content: space-between">
          <a-link class="register" href="/user/register">没有账号？</a-link>
          <a-button
            class="login"
            html-type="submit"
            shape="round"
            type="primary"
          >
            登录
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import {
  CheckCodeControllerService,
  UserControllerService,
  UserLoginRequest,
} from "../../../generated";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { Message } from "@arco-design/web-vue";

const router = useRouter();
const store = useStore();

// 获取验证码
let checkCodeImage = ref("");
const getCheckCode = async () => {
  const res = await CheckCodeControllerService.generatePicCheckCodeUsingPost();
  form.checkCodeKey = res.key;
  checkCodeImage.value = res.aliasing;
};

/*切换验证码*/
const toggleCheckCode = () => {
  getCheckCode();
};

onMounted(() => {
  getCheckCode();
});

// 登录表单数据
const form: UserLoginRequest = reactive({
  userAccount: "",
  userPassword: "",
  checkCodeKey: "",
  checkCode: "",
});

// 处理登录
const handleSubmit = async () => {
  // 发生登录请求
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    // 登录成功，获取并存储用户信息，跳转到首页
    const token = res.data.token;
    localStorage.setItem("token", token);
    const userVOInfo = res.data.userVOInfo;
    await store.dispatch("user/saveLoginUser", userVOInfo);
    // replace: true 防止用户跳转之后点击回退到登录页
    await router.push({ path: "/", replace: true });
  } else {
    // 登录失败，提示用户失败原因
    Message.error("登录失败，" + res.message);
  }
};
</script>

<style scoped>
/*登录*/
.login {
  width: 120px;
}

/*验证码*/
.checkCode {
  margin-left: 10px;
  cursor: pointer;
}
</style>